/* 
 * File:   parallel.h
 * Author: saud
 *
 * Created on March 21, 2011, 3:24 PM
 */
#include<stdio.h>

#ifndef PARALLEL_H
#define	PARALLEL_H

#ifndef _PTHREAD_H
#include<pthread.h>
#endif

class parallel_data
{
private:

    static bool Threads_Exit_flag;
    static pthread_mutex_t Threads_Exit_flag_mux;

    static void* global_trial;
    static pthread_mutex_t global_trial_mux;


    inline static bool Get_Exit_Flag()
    {
        return Threads_Exit_flag;
    }

    inline static void Set_Exit_Flag(bool Value)
    {

        pthread_mutex_lock(&Threads_Exit_flag_mux);
        Threads_Exit_flag=Value;
        pthread_mutex_unlock(&Threads_Exit_flag_mux);
        if(Threads_Exit_flag==true)
        {
            printf("Exit flag has been set\n");
        }
    }

public:

    static void init_members(int nvar)
    {
        Threads_Exit_flag = false;
        pthread_mutex_init(&Threads_Exit_flag_mux,NULL);

        global_trial = new bool[nvar];
        pthread_mutex_init(&global_trial_mux,NULL);

    }

    static void exit_thread(int index)
    {
        Set_Exit_Flag(true);
        printf("Thread[%d] has found the answer\n",index);
        //pthread_exit((void*)1);
    }

    inline static void check_other_threads(int index)
    {
        if(Get_Exit_Flag()==true)
        {
            printf("Thread[%d] has been forced to exit\n",index);
            pthread_exit((void*)0);
        }
    }
    


};

#endif	/* PARALLEL_H */

#ifndef _GLOBAL_VARS
    #define _GLOBAL_VARS


#endif

